$(function(){
    $.ajax({
        url:'system/config!queryType.action',
        type: "GET",
        dataType: "json"
    }).done(function(data) {
        if(data&&data.length>0){
            for(var i=0;i<data.length;i++) {
                if(!$('#typeTabs').tabs('exists',data[i].name)){
                    $('#typeTabs').tabs('add',{
                        id:data[i].id,
                        title:data[i].name,
                        content:function(){
                            return "<table id='list_"+data[i].id+"' class='list'></table>";
                        },
                        selected:i==0
                    });
                }else{
                    $('#typeTabs').tabs('select',data[i].name);
                }
            }
        }
    });
    $('#typeTabs').tabs({
        onSelect: function(title,index){
            var tab=$('#typeTabs').tabs('getTab',index);
            queryContent(tab.panel("options").id);
        },
        onClose:function(title){
            return false;
        }
    });
});

function queryContent(id) {
    $("#list_"+id).mothinDatagrid({
        url:'system/config!query.action?systemConfigType.id='+id,
        sortName: 'name',
        sortOrder: 'desc',
        columns:[[
            {field:'name',title:'名称',width:200,fixed:true,sortable:true},
            {field:'value',title:'值',width:200,fixed:true,sortable:true},
            {field:'memo',title:'说明',width:300,sortable:true},
            {field:'id',title:'操作',width:0,fixed:true,resizable:false,formatter:function(value,row){
                var data=frame_queryOperation(History.getState().url);
                if(data&&data.rows&&data.rows.length>0){
                    var link="";
                    for(var i=0;i<data.rows.length;i++){
                        link+='<button class="btn btn-link btn-xs" onclick="handle(\''+id+'\',\''+data.rows[i].action+'\',\''+data.rows[i].name+'\',\''+value+'\');"><span class="'+data.rows[i].ico+'" />&nbsp;'+data.rows[i].name+'</button>';
                    }
                    return link;
                }
            }},
        ]]
    });
}

function handle(id,action,name,bizId) {
    switch(action){
        case 'system/config!update.action':
            showDialog(action+"?systemConfigDetail.id="+bizId+"&__t="+$.mothin.util.getStamp(),'修改配置',800,400,function(){
                $("#list_"+id).datagrid("reload");
            });
            break;
    }
}